<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //
        function timeoutPromise(delay) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    //   console.log(1)
                    resolve(`${delay}`)
                }, delay * 1000)
            })
        }
        const p1 = timeoutPromise(1)
        const p2 = timeoutPromise(2)
        const p3 = timeoutPromise(3)
        const p4 = timeoutPromise(4)
        const p5 = new Promise((resolve, reject) => {
            setTimeout(() => {
                reject('失败了')
            }, 1000)
        })
        // 1. Promise.all里面传的是一个数组（ 数组的每一项都是一个promise）,
        //     只有每一个promise都成功了， 才会走进.then, 有一个失败了就会走进.catch
        const a = Promise.all([p1, p2, p3, p4]).then((res) => {
            console.log(res);
        })
        const b = Promise.all([p1, p2, p3, p4, p5]).then((res) => {
            console.log(res);
        }).catch((err) => {
            console.log(err);
        })

        //2.Promise.race里面传的也是数组 里面每一项也是promise 
        //等待最先成功的哪个 只要有一个成功就会走进.then 全部失败走进catch
        Promise.race([p1, p2, p3, p4]).then((res) => {
            console.log(res);
        })
    </script>
</body>

</html>